﻿using ConfExpert.Datasets;
using ConfExpert.Forms.Base;
using ConfExpert.Utility;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;
using DevTeam;

using System;
using System.Windows.Forms;

namespace ConfExpert.Forms.Depo.Westbrook
{
    public partial class COOrder : XForm
    {
        public COOrder()
        {
            InitializeComponent();
        }

        BindingSource bsOrders;
        int DagilimId = 0;
        private void COOrder_Load(object sender, EventArgs e)
        {
            
            bsOrders= gvCOOrders.Init("GetOrdersFromCO");
            gvCOOrders.AddDetailGrid(gvDagilim, "OrderId");

            gvCOOrders.AddColumn("Total", typeof(int), "SUM(Child({FK}).TotalComputed)");

            RepositoryItemGridLookUpEdit riDagilim = gvCOOrders.Columns["DagilimId"].ColumnEdit as RepositoryItemGridLookUpEdit;
            gvCOOrders.Columns["DagilimId"].ColumnEdit = riDagilim;
            riDagilim.Closed += riDagilim_Closed;
            bsOrders.CurrentChanged += bsOrders_CurrentChanged;
            DagilimId = bsOrders.GetCurrentValue("DagilimId").ToInt(-1);
            DagilimChanged();

            gvCOOrders.MakeColumnInvisible("OrderId");
            gvCOOrders.YanYanaGetir("Total:Pos");
            gvDagilim.AddSummary("A01", "A02", "A03", "A04", "A05", "A06", "A07", "A08", "A09", "A10",
                            "A11", "A12", "A13", "A14", "A15", "A16", "A17", "A18", "A19", "A20",
                            "A21", "A22", "A23", "A24", "A25", "A26", "A27", "A28");
            btnGuncelle.ItemClick += btnGuncelle_ItemClick;
            gvCOOrders.BestFitColumns();
            gvDagilim.BestFitColumns();
            
        }

        void btnGuncelle_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gvCOOrders.RefreshDataSource(true, "GetOrdersFromCO");
        }

        private void DagilimChanged()
        {
            try
            {
                gvDagilim.CloseEditor();
                gvDagilim.BeginUpdate();

                MyData.DagilimRow r = AppSession.Lookups.Dagilim.FindByDagilimId(DagilimId);
                if (r != null)
                    for (int i = 1; i <= 28; i++)
                    {
                        string fieldName = String.Format("A{0:00}", i);
                        string cap = r[String.Format("A{0:00}", i)].ToString();

                        if (cap != "")
                        {
                            gvDagilim.Columns[fieldName].Caption = cap;
                        }
                        else
                            gvDagilim.Columns[fieldName].Caption = " ";
                    }
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
            finally
            {
                gvDagilim.EndUpdate();
                gvDagilim.BestFitColumns();
            }
        }

        void bsOrders_CurrentChanged(object sender, EventArgs e)
        {
            DagilimId = bsOrders.GetCurrentValue("DagilimId").ToInt(-1);
            DagilimChanged();
        }

        void riDagilim_Closed(object sender, DevExpress.XtraEditors.Controls.ClosedEventArgs e)
        {
            DagilimId = (sender as BaseEdit).EditValue.ToInt(-1);
            DagilimChanged();
        }

    }
}